Imports System.Data

Partial Class Modules_Questionnaires_Controls_cfgMultipleChoicesSingleAnswer
    Inherits System.Web.UI.UserControl
    Implements DotNetPM.Business.Questionnaire.UserControls.IConfigUC


#Region "IConfigUC's"

    Private _AnswerType As Integer
    Public Property AnswerType() As Integer Implements DotNetPM.Business.Questionnaire.UserControls.IConfigUC.AnswerType
        Get
            Return _AnswerType
        End Get
        Set(ByVal value As Integer)
            _AnswerType = value
        End Set
    End Property

    Public Property Config() As String Implements DotNetPM.Business.Questionnaire.UserControls.IConfigUC.Config
        Get
            Dim build As New StringBuilder
            build.Append(_AnswerType)
            build.Append("||" & ddlControl.SelectedIndex)
            build.Append("||" & rRequired.SelectedIndex)
            If ddlControl.SelectedIndex Then
                build.Append("||" & txtColumns.Text)
                build.Append("||" & ddlDirection.SelectedIndex)
                build.Append("||" & ddlLayout.SelectedIndex)
            End If
            Return build.ToString
        End Get
        Set(ByVal value As String)
            Dim val As String() = Split(value, "||")
            _AnswerType = val(0)
            Integer.TryParse(val(1), ddlControl.SelectedIndex)
            Integer.TryParse(val(2), rRequired.SelectedIndex)
            If ddlControl.SelectedIndex Then
                txtColumns.Text = val(3)
                Integer.TryParse(val(4), ddlDirection.SelectedIndex)
                Integer.TryParse(val(5), ddlLayout.SelectedIndex)
            End If
        End Set
    End Property


    Public Sub Reset() Implements DotNetPM.Business.Questionnaire.UserControls.IConfigUC.Reset
        ' Call this function at the main page to clear all values in controls
        ' When moving to the new question.
        dt.Clear()
        lstChoices.Items.Clear()
        Session("Choices") = dt
        txtColumns.Text = 0
        'Response.Write("Reset<br/>")
    End Sub

#End Region


#Region "Private"

    Private dt As DataTable
    Private _editing As Boolean = False

    Private Sub InitLoadData()
        dt = CType(Session("Choices"), DataTable)
        If dt Is Nothing Then
            dt = New DataTable
            dt.Columns.Add("SortID")
            dt.Columns.Add("Choice")
        End If
    End Sub

    Private Sub BindChoiceList()
        If dt.Rows.Count > 0 Then
            Dim dv As DataView = dt.DefaultView
            dv.Sort = "SortID"
            lstChoices.DataSource = dv
            lstChoices.DataTextField = "Choice"
            lstChoices.DataValueField = "SortID"
        End If
        lstChoices.DataBind()
    End Sub

#End Region


#Region "Buttons"

    Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click
        '' Remember the selection if any.
        'Dim selectedText As String = String.Empty
        'If lstChoices.SelectedIndex > -1 Then
        '    Response.Write("Selected item = " & lstChoices.SelectedItem.Text)
        '    selectedText = lstChoices.SelectedItem.Text
        'End If

        ' Add new row
        If txtInput.Text.Trim <> "" Then
            Dim dr As DataRow = dt.NewRow
            If dt.Rows.Count > 0 Then
                dr("SortID") = dt.Rows(dt.Rows.Count - 1).Item("SortID") + 1
            Else
                dr("SortID") = 0
            End If
            dr("Choice") = txtInput.Text
            dt.Rows.Add(dr)
            Session("Choices") = dt
            BindChoiceList()
            txtInput.Text = ""
            hEditIndex.Value = ""
        End If

        lstChoices.SelectedIndex = lstChoices.Items.Count - 1
        txtInput.Focus()

        '' Apply the selection if any.
        'If Not String.IsNullOrEmpty(selectedText) Then _
        '    lstChoices.Items.FindByText(selectedText).Selected = True

        'Response.Write("ButAdd_Click<br/>")
    End Sub

    Private Sub butDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butDelete.Click
        ' Check if any is selected.
        If lstChoices.SelectedIndex > -1 Then
            dt.Rows.Remove(dt.Select("SortID=" & lstChoices.SelectedValue)(0)) 'RemoveAt(lstChoices.SelectedIndex)
            hEditIndex.Value = ""
            Session("Choices") = dt
            BindChoiceList()
        End If
        'Response.Write("butDelete_Click<br/>")
    End Sub

    Private Sub butEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butEdit.Click
        ' Check if any is selected.
        If lstChoices.SelectedIndex > -1 Then
            hEditIndex.Value = lstChoices.SelectedValue
            txtInput.Text = dt.Select("SortID=" & hEditIndex.Value)(0).Item("Choice") 'dt.Rows(lstChoices.SelectedIndex).Item("Choice")
            lstChoices.Enabled = False
            EnableButs(False)
            _editing = True
        End If
        'Response.Write("butEdit_Click: lstChoices.SelectedIndex = " & lstChoices.SelectedIndex & "<br/>")
    End Sub

    Private Sub ButUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButUpdate.Click
        dt.Select("SortID=" & hEditIndex.Value)(0).Item("Choice") = txtInput.Text
        Session("Choices") = dt
        BindChoiceList()
        txtInput.Text = ""
        lstChoices.SelectedValue = hEditIndex.Value
        'Response.Write("ButUpdate_Click<br/>")
    End Sub

    Protected Sub butUp_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butUp.Click
        ' Check if any is selected.
        If lstChoices.SelectedIndex > 0 Then
            Dim curIndex As Integer = lstChoices.SelectedIndex
            Dim SID As Integer = lstChoices.SelectedValue
            Dim upSID As Integer = lstChoices.Items(curIndex - 1).Value
            dt.Select("SortID=" & SID)(0).Item("SortID") = 255
            dt.Select("SortID=" & upSID)(0).Item("SortID") = SID
            dt.Select("SortID=255")(0).Item("SortID") = upSID
            Session("Choices") = dt
            BindChoiceList()
            lstChoices.SelectedIndex = curIndex - 1
        Else
            lblError.Visible = True
            'lstChoices.SelectedIndex = CInt(hEditIndex.Value)
            lblError.Text = "The selection is already on the top."
        End If
    End Sub

    Protected Sub butDown_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butDown.Click
        ' Check if any is selected.
        If lstChoices.SelectedIndex < lstChoices.Items.Count - 1 Then
            Dim curIndex As Integer = lstChoices.SelectedIndex
            Dim SID As Integer = lstChoices.SelectedValue
            Dim downSID As Integer = lstChoices.Items(curIndex + 1).Value
            dt.Select("SortID=" & SID)(0).Item("SortID") = 255
            dt.Select("SortID=" & downSID)(0).Item("SortID") = SID
            dt.Select("SortID=255")(0).Item("SortID") = downSID
            Session("Choices") = dt
            BindChoiceList()
            lstChoices.SelectedIndex = curIndex + 1
        Else
            lblError.Visible = True
            'lstChoices.SelectedIndex = CInt(hEditIndex.Value)
            lblError.Text = "The selection is already at the buttom."
        End If
    End Sub

    Private Sub EnableButs(ByVal enable As Boolean)
        'ButAdd.Enabled = enable
        butEdit.Enabled = enable
        butDelete.Enabled = enable
        butUp.Enabled = enable
        butDown.Enabled = enable
        ButUpdate.Enabled = Not enable
        'Response.Write("EnableButs = " & lstChoices.Items.Count & " : ")
    End Sub
#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitLoadData()
        BindChoiceList()
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        If Not _editing Then
            If lstChoices.Items.Count > 0 Then
                EnableButs(True)
            Else
                EnableButs(False)
                ButUpdate.Enabled = False
            End If
            Dim _visible As Boolean = CBool(ddlControl.SelectedIndex)
            'trRequired.Visible = _visible
            trRepeatColumns.Visible = _visible
            trRepeatDirection.Visible = _visible
            trRepeatLayout.Visible = _visible
        End If
        'Response.Write("Page_PreRender<br/>")
    End Sub
End Class
